package com.beiing.tianshuai.tianshuai.mine.ui;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.adapter.MyDiscoverAdapter;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.CodeBean;
import com.beiing.tianshuai.tianshuai.entity.MyDiscoveryBean;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.beiing.tianshuai.tianshuai.freshnews.DiscoveryDetailActivity;
import com.beiing.tianshuai.tianshuai.mine.presenter.MyDiscoverPresenter;
import com.beiing.tianshuai.tianshuai.mine.view.MyDiscoverViewImpl;
import com.beiing.tianshuai.tianshuai.util.DensityUtils;
import com.beiing.tianshuai.tianshuai.widget.CustomDialog;
import com.beiing.tianshuai.tianshuai.widget.CustomProgressDialog;
import com.beiing.tianshuai.tianshuai.widget.MyRefreshHeader;
import com.beiing.tianshuai.tianshuai.widget.RecycleViewDivider;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyDiscoverActivity extends BaseActivity implements MyDiscoverViewImpl{

    @BindView(R.id.toolbar_navigation)
    ImageView mToolbarNavigation;
    @BindView(R.id.dynamic_header)
    MyRefreshHeader mDynamicHeader;
    @BindView(R.id.txt_empty_warning)
    TextView mTxtEmptyWarning;
    @BindView(R.id.rv_my_discover)
    RecyclerView mRvMyDiscover;
    @BindView(R.id.srl_my_refresh_layout)
    SmartRefreshLayout mSrlMyRefreshLayout;

    private static final String                 TAG = "MyDiscoverActivity";
    private Context                             mContext;
    private List<MyDiscoveryBean>               mDiscovers;
    private MyDiscoverAdapter                   mAdapter;
    private MyDiscoverPresenter                 mPresenter;
    private int                                 mDelPosition;
    private String mUid;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_discover;
    }

    @Override
    protected void init() {
        ButterKnife.bind(this);
        mContext = this;
        mUid = UserInfoBean.getUid(mContext);
        initToolbar();
        initRefreshLayout();
        initRecyclerView();
        initListener();
        initPresenter();
    }

    /**
     * 初始化Toolbar
     */
    private void initToolbar() {
        mToolbarNavigation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    /**
     * 初始化刷新控件
     */
    private void initRefreshLayout() {
        mSrlMyRefreshLayout.setRefreshHeader(new MyRefreshHeader(mContext));
        mSrlMyRefreshLayout.setEnableLoadmore(false);
        mSrlMyRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
            }
        });
    }

    /**
     * 初始化列表
     */
    private void initRecyclerView() {
        mDiscovers = new ArrayList<>();
        mAdapter = new MyDiscoverAdapter(mContext);
        mRvMyDiscover.setAdapter(mAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mRvMyDiscover.setNestedScrollingEnabled(false);
        mRvMyDiscover.setLayoutManager(linearLayoutManager);
        //绘制item间的分割线，
        mRvMyDiscover.addItemDecoration(
                new RecycleViewDivider(
                        mContext, LinearLayoutManager.HORIZONTAL, DensityUtils.dp2px(mContext, 0.5f),
                        ContextCompat.getColor(mContext, R.color.common_gray_background))
        );
    }

    private void initListener() {
        //刷新
        mSrlMyRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mPresenter.getMyDiscoveries(mUid);
            }
        });

        //删除
        mAdapter.setOnDelClickListener(new MyDiscoverAdapter.OnDelClick() {
            @Override
            public void onDelClickListener(final MyDiscoveryBean.DataBean.NumberBean bean, final int position) {
                CustomDialog dialog = new CustomDialog(mContext, R.style.dialog, "确定要删除吗？", new CustomDialog.OnCloseListener() {
                    @Override
                    public void onClick(Dialog dialog, boolean confirm) {
                        if (confirm) {
                            mDelPosition = position;
                            mPresenter.getDelDiscoveryResult(bean.getId());
                            dialog.dismiss();
                        } else {
                            dialog.dismiss();
                        }
                    }
                }).setTitle("提示");
                dialog.setCanceledOnTouchOutside(true);
                dialog.show();
            }

            @Override
            public void onItemClickListener(MyDiscoveryBean.DataBean.NumberBean bean, int position) {
                Intent intent = new Intent(mContext, DiscoveryDetailActivity.class);
                intent.putExtra("fresh_id", bean.getId());
                startActivity(intent);
            }
        });
    }

    private void initPresenter() {
        mPresenter = new MyDiscoverPresenter(this);
        mPresenter.getMyDiscoveries(mUid);
    }

    @Override
    public void showProgress() {
        if (!mSrlMyRefreshLayout.isRefreshing()) {
            CustomProgressDialog.showLoading(mContext, "正在努力加载，请稍后...", true);
        }
    }

    @Override
    public void hideProgress() {
        CustomProgressDialog.stopLoading();
    }

    @Override
    public void onRequestSuccess(MyDiscoveryBean bean) {
        List<MyDiscoveryBean.DataBean.NumberBean> discoveries = bean.getData().getNumber();
        if (discoveries != null && discoveries.size() > 0) {
            mTxtEmptyWarning.setVisibility(View.GONE);
            mAdapter.updateData(discoveries);

        } else {
            mTxtEmptyWarning.setVisibility(View.VISIBLE);
        }
        if (mSrlMyRefreshLayout.isRefreshing()) {
            mSrlMyRefreshLayout.finishRefresh();
        }
    }

    @Override
    public void onRequestFailed(Throwable throwable) {
        showShortText("请求失败");
    }

    @Override
    public void onDelSuccess(CodeBean bean) {
        int code = bean.getCode();
        if (code == 200) {
            mAdapter.removeData(mDelPosition);
            List<MyDiscoveryBean.DataBean.NumberBean> list = mAdapter.getList();
            if (list.size() == 0) {
                mTxtEmptyWarning.setVisibility(View.VISIBLE);
            }
        }
    }
}
